/**
 * 仅用于职位映射审批流程
 */
import React from 'react';
import DataUpdate from '@/components/DataUpdate';
import { withRouter } from 'umi';
import { Pagination } from '@/components';
import useEditionDetailList from './useEditionDetailList';

const PostTableDiff = React.forwardRef(({ value, type, onChange = () => {}, ...rest }, ref) => {
    const [pagination, setPagination] = React.useState({ current: 1, pageSize: 10 });
    const [total, dataSource, loading] = useEditionDetailList(value, pagination); // 版本列表详情数据

    React.useEffect(() => {
        onChange(value);
    }, [value]);

    const formatter = text => String(text ?? '').replace(/\B(?=(\d{3})+(?!\d))/g, ',');

    const columns = [
        {
            title: '微微职位类别',
            dataIndex: 'firstPositionCodeName',
            render: (text, record) => {
                if (record.firstPositionCodeName && record.secondPositionCodeName) {
                    return `${record.firstPositionCodeName}-${record.secondPositionCodeName}`;
                }
                return '';
            }
        },
        {
            title: '微微职位名称',
            dataIndex: 'postName'
        },
        {
            title: '微微职级',
            dataIndex: 'gradeCode'
        },
        {
            title: '微微职位代码',
            // width: 220,
            dataIndex: 'postCode'
        },
        {
            title: '参考数据名称',
            // width: 220,
            dataIndex: 'salaryStandardName'
        },
        {
            title: '参考职位类别',
            // width: 220,
            dataIndex: 'postTypeName'
        },
        {
            title: '参考职位名称',
            // width: 120,
            dataIndex: 'refPostName'
        },
        {
            title: '参考职位代码',
            dataIndex: 'refSalaryPostCode'
        },
        {
            title: '一线城市90分位',
            dataIndex: 'firstLineNineQuantile',
            formatter
        },
        {
            title: '一线城市75分位',
            dataIndex: 'firstLineSevenQuantile',
            formatter
        },
        {
            title: '一线城市50分位',
            dataIndex: 'firstLineFiveQuantile',
            formatter
        },
        {
            title: '一线城市25分位',
            dataIndex: 'firstLineTwoQuantile',
            formatter
        },
        {
            title: '二线城市90分位',
            dataIndex: 'secondLineNineQuantile',
            formatter
        },
        {
            title: '二线城市75分位',
            dataIndex: 'secondLineSevenQuantile',
            formatter
        },
        {
            title: '二线城市50分位',
            dataIndex: 'secondLineFiveQuantile',
            formatter
        },
        {
            title: '二线城市25分位',
            dataIndex: 'secondLineTwoQuantile',
            formatter
        }
    ];

    const handlePaginationChange = _pagination => {
        setPagination({ ...pagination, ..._pagination });
    };

    return (
        <>
            <DataUpdate
                getSource={{
                    dataSource,
                    columns
                }}
                tableConfig={{
                    modifyType: 'modifyType',
                    difference: 'difference'
                }}
                tableProps={{
                    scroll: { x: 'max-content' },
                    rowKey: 'id',
                    size: 'small',
                    loading
                }}
                ref={ref}
                {...rest}
            />
            {total > 10 && <Pagination total={total} onChange={handlePaginationChange} {...pagination} />}
        </>
    );
});

export default withRouter(PostTableDiff);
